/*
 * Copyright (c)$today.year.month.day
 *  admin:郑程荣.
 *  文件名:ProjectListActivity.java
 *  项目功能:
 */

package com.jg.myproject.module.projectinfo;

import android.content.Intent;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.jg.myproject.api.BaseObserver;
import com.jg.myproject.api.Const;
import com.jg.myproject.api.RetrofitFactory;
import com.jg.myproject.api.RxSchedulers;
import com.jg.myproject.api.bean.BaseEntity;
import com.jg.myproject.api.bean.CommonRequestBean;
import com.jg.myproject.api.bean.reponse.CommonReponseBean;
import com.jg.myproject.module.actualtime.ActualTimeDataActivity;
import com.jg.myproject.module.base.BaseActivity;
import com.jg.myproject.module.home.HomeActivity;
import com.jg.myproject.presenter.ProjectInfoPresenter;
import com.jg.myproject.presenter.ProjectListPresenter;
import com.jg.myproject.pview.IProjectListView;
import com.jg.myproject.utils.RxSPTool;
import com.zcr.myproject.R;

import java.util.ArrayList;

import butterknife.BindView;
import io.reactivex.Observable;

/**
 * Created by llz on 2018/11/19.
 */
public class ProjectListActivity extends BaseActivity implements IProjectListView{
    @BindView(R.id.rv_content)
    RecyclerView mRvContent;
    @BindView(R.id.swipeRefreshLayout)
    SwipeRefreshLayout mSwipeRefreshLayout;
    private ArrayList<CommonReponseBean> mBeans;
    private BaseQuickAdapter mBaseQuickAdapter;
    public int mNextRequestPage = 1;
    ProjectListPresenter mProjectListPresenter;
    public boolean mIsRefresh;
    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_project_list;
    }
    @Override
    protected void initViews() {
        initTitle(false, "工程列表");
        mProjectListPresenter = new ProjectListPresenter(this);
        mSwipeRefreshLayout.setColorSchemeResources(R.color.main_color);
        mRvContent.setLayoutManager(new LinearLayoutManager(this));
        mBeans = new ArrayList<CommonReponseBean>();

        mBaseQuickAdapter = new BaseQuickAdapter<CommonReponseBean, BaseViewHolder>(R.layout.item_project_list, mBeans) {
            @Override
            protected void convert(BaseViewHolder baseViewHolder, CommonReponseBean bean) {
                TextView tv_01 = baseViewHolder.getView(R.id.tv_01);
                TextView tv_02 = baseViewHolder.getView(R.id.tv_02);
                TextView tv_03 = baseViewHolder.getView(R.id.tv_03);
                tv_01.setText(baseViewHolder.getAdapterPosition() + 1 + "");
                tv_02.setText(bean.getGcName());
                tv_03.setText(bean.getMonitorPartName());
            }
        };
        mBaseQuickAdapter.setEnableLoadMore(true);
        mBaseQuickAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                mIsRefresh = mNextRequestPage == 1;
                getData();
            }
        }, mRvContent);
        initRefreshLayout(); // 下拉刷新
        mRvContent.setAdapter(mBaseQuickAdapter);
        mBaseQuickAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Intent intent = new Intent(ProjectListActivity.this, HomeActivity.class);
                RxSPTool.putString(ProjectListActivity.this,Const.PROJECTID,mBeans.get(position).getGcId()); //缓存工程ID
                RxSPTool.putString(ProjectListActivity.this,Const.PROJECTNAME,mBeans.get(position).getGcName()); //工程名称
                RxSPTool.putString(ProjectListActivity.this,Const.MONITORPARTID,mBeans.get(position).getMonitorPartId()); //监测部位id
                RxSPTool.putString(ProjectListActivity.this,Const.MONITORPARTCODE,mBeans.get(position).getMonitorPartCode()); //监测部位编码
                RxSPTool.putString(ProjectListActivity.this,Const.MONITORPARTNAME,mBeans.get(position).getMonitorPartName()); //监测部位
                startActivity(intent);
            }
        });

    }


    @Override
    protected void updateViews(boolean isRefresh) {
        mIsRefresh = true;
        getData();
    }

    private void initRefreshLayout() {
        mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                mNextRequestPage = 1;
                mIsRefresh = true;
                mBaseQuickAdapter.setEnableLoadMore(false);//这里的作用是防止下拉刷新的时候还可以上拉加载
                getData();
            }
        });
    }
   @Override
    public void getReponseBeans(BaseEntity<CommonReponseBean> bean) {

        mNextRequestPage++;
        int size = bean.getBeans() == null ? 0 : bean.getBeans().size();

       if (mIsRefresh) {
           mBeans.clear();
           mBeans.addAll(bean.getBeans());
           mBaseQuickAdapter.setNewData(mBeans);
           mBaseQuickAdapter.setEnableLoadMore(true);
           mSwipeRefreshLayout.setRefreshing(false);
        } else {
            if (size > 0) {
                mBeans.addAll(bean.getBeans());
                mBaseQuickAdapter.addData(mBeans);
            }
           // mSwipeRefreshLayout.setRefreshing(true);
        }
        if (size < Const.PAGE_SIZE) {
            //第一页如果不够一页就不显示没有更多数据布局
            mBaseQuickAdapter.loadMoreEnd(mIsRefresh);
        } else {
            mBaseQuickAdapter.loadMoreComplete();
        }
    }

    private void getData() {
        CommonRequestBean commonRequestBean = new CommonRequestBean();
        String userId = RxSPTool.getString(ProjectListActivity.this,Const.USERID);
        commonRequestBean.setUserId(userId);
        commonRequestBean.setPageSize(Const.PAGE_SIZE);
        commonRequestBean.setPageNo(mNextRequestPage);
        mProjectListPresenter.getData(commonRequestBean);//获取数据
    }

}
